#region

using System;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

#endregion

namespace RedEye.CMA.CodeTest
{
	[TestFixture]
	public class PatternMatcherFixture
	{
		private const string TEST_SENTENCE =
			"Polly put the kettle on, polly put the kettle on, polly put the kettle on we'll all have tea";

		private PatternMatcher _patternMatcher;

		[SetUp]
		public void main_setup()
		{
			_patternMatcher = new PatternMatcher(TEST_SENTENCE);
		}


		[Test]
		public void should_match_Polly()
		{
			var expectedResult = new[] {1, 26, 51};
			assert_match("Polly", expectedResult);
		}

		[Test]
		public void should_match_polly()
		{
			var expectedResult = new[] {1, 26, 51};
			assert_match("polly", expectedResult);
		}

		[Test]
		public void should_match_ll()
		{
			var expectedResult = new[] {3, 28, 53, 78, 82};
			assert_match("ll", expectedResult);
		}

		[Test]
		public void should_match_Ll()
		{
			var expectedResult = new[] {3, 28, 53, 78, 82};
			assert_match("Ll", expectedResult);
		}


		[Test]
		public void should_not_match_Polx()
		{
			var expectedResult = new int[0];
			assert_match("Polx", expectedResult);
		}

		[Test]
		public void should_not_match_X()
		{
			var expectedResult = new int[0];
			assert_match("Polx", expectedResult);
		}

		private void assert_match(string subText, int[] expectedResult)
		{
			int[] result = _patternMatcher.Match(subText);
			pretty_print_result(result);
			Assert.That(result, Is.EqualTo(expectedResult));
		}

		private void pretty_print_result(int[] result)
		{
			if (result == null || result.Length == 0)
			{
				Console.WriteLine("<no match>");
				return;
			}

			var sb = new StringBuilder();
			result.ToList()
				.ForEach(x =>
				         	{
				         		sb.Append(x);
				         		sb.Append(", ");
				         	});
			Console.WriteLine("[{0}]", sb.ToString(0, sb.Length - 2));
		}
	}
}